<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-8-22
 * Time: 上午10:54
 */
$_REQUEST['subsite'] = 'backend';
require ('core/shop.php');
//查询订单主表类
quanli_require('module/base/bill/billSearch.class.php');
//字典操作类 itemHandler
quanli_require('module/base/system/itemHandler.class.php');

if (!isset($_REQUEST['bill_no']) || empty($_REQUEST['bill_no'])) {
    die('缺少参数');
}

$bill_array = array(); //订单商品数组

//查询打印商品信息
$arg_array = array(
    'bill_no' => $_REQUEST['bill_no']
);

//订单查询类
$bill_arraySearch = new billSearch();

//查询订单主表信息 bill_master
$temp_array  = $bill_arraySearch->searchQueryBill( $arg_array );
$temp_array_new=$temp_array;

//实例item类
$itemHandler = new itemHandler();
$item_array['itemID']=$temp_array[0]['billPromotion'];
//查询促销渠道
$query=$itemHandler->searchItem( $item_array );
$value=mb_substr($query[0]['itemName'],-2);

$Has_ptp05 = false;//是否存在异地服务费





//查询订单商品表 bill_goods
$bill_goods_array = $bill_arraySearch->searchBillGoods( $arg_array );

if ( $temp_array ) {
    $bill_array= $temp_array[0];
    $bill_array_code = substr($temp_array[0]['bill_code'],-7);//后六位订单号
    //$bill_array_code = $temp_array[0]['bill_code'];
    $bill_array['bill_code_substr'] = $bill_array_code;
    $bill_array['bill_goods'] = $bill_goods_array;
}

$goods_array = array();//订单商品信息数组
$bill_card_array = array();//生日牌数组
$bill_candle_amount = 0; //赠送蜡烛数量
$bill_extra_candle_amount = 0; //额外蜡烛数量
$bill_extra_tableware_amount = 0; //额外餐具数量
$goods_array_count = 0;//商品信息数组数量
$bill_goods_total_count=count($bill_goods_array);
foreach ( $bill_goods_array as $k=>$val ){
    $goods_array_count ++;
    if( $goods_array_count <= 8){
        $goods_array[] = array(
            'TITLE' => $val['goods_name'],
            'SKU_PROPERTIES_NAME' => $val['goods_spec'],
            'NUM' => $val['goods_amount'],
            'SP' => $val['goods_storePrice'],
            'DISCOUNT' => round($val['goods_purchasePrice']/$val['goods_storePrice']*100,2).'%',
            'PRICE' => $val['goods_purchasePrice'],
//            'USEINGEGRAL' => 0
        );


    }
    if( $val['birthday_card'] ){
        $bill_card_array[] = $val['birthday_card'];
    }
    $bill_candle_amount += $val['candle_amount'];
    $bill_extra_candle_amount += $val['extra_candle_amount'];
    $bill_extra_tableware_amount += $val['extra_tableware_amount'];
    if($val['product_code']=='ptp05')
    {
        $Has_ptp05=true;
    }
}

if( count($bill_goods_array) > 8 )
{
    $goods_array[7]['TITLE']="蛋糕总数量是：".$bill_goods_total_count;
    $goods_array[7]['SKU_PROPERTIES_NAME']="";
    $goods_array[7]['NUM']="";
    $goods_array[7]['SP']="";
    $goods_array[7]['DISCOUNT']="";
    $goods_array[7]['PRICE']="";
}



$SELLERMESSAGE = $bill_extra_tableware_amount>0 ?( (empty($bill_array['remark'])?"":$bill_array['remark'].",") ."额外餐具*$bill_extra_tableware_amount" ): $bill_array['remark'];//配送备注

$bill_array['send_start_time'] = substr($bill_array['send_start_time'],0,5);
$bill_array['send_end_time'] = substr($bill_array['send_end_time'],0,5);
$bill_Special_status_flg ='';
error_log(print_r($bill_array['special_status'],1));
if($bill_array['special_status'] == 2){
    $bill_Special_status_flg = '※';
}
$PRTIME = ( $bill_array['send_end_time']=="00:00" || $bill_array['send_end_time']==$bill_array['send_start_time'] ) ? $bill_array['send_start_time'] : ( $bill_array['send_start_time'].'-'.$bill_array['send_end_time'] );//配送时间
$PRTIME = $PRTIME . $bill_Special_status_flg;
$PAYTYPE = $bill_array['pay_type'] == 1?( intval( $bill_array['payment_method_id'])>0 ? $bill_array['payment_method_name'] : $bill_array['payTypeName'] ) :$bill_array['payTypeName'];

$TOUBU='送货单';
$TOUBU2='收货人';
$TOUBU1='收货地址';
$PHONE=$bill_array['deliver_mobile']?$bill_array['deliver_mobile']:$bill_array['deliver_phone'];//联系电话

$send_date_1 = $bill_array['send_date'];
$RECEIVNAME_1 = $bill_array['deliver_name'];
$ALL_RECEIVADDRESS_1 = htmlspecialchars($bill_array['deliver_city'].$bill_array['deliver_county'].$bill_array['deliver_address'], ENT_QUOTES);

if($bill_array['billType']==9){
    $TOUBU='试吃单';
}else if($Has_ptp05){

    //结款信息
    $TOUBU='结款单';
    $TOUBU2='结款人';
    $TOUBU1='结款地址';
//    $bill_other_address_pay = new entity('bill_other_address_pay');
//    $bill_other_address_pay->bill_no=$_REQUEST['bill_no'];
//    $G_SHOP->DBCA->fetch($bill_other_address_pay);
//    $send_date_1=$bill_other_address_pay->date;
//    $RECEIVNAME_1=$bill_other_address_pay->custName;
//    $PHONE=$bill_other_address_pay->mobile;
//    $ALL_RECEIVADDRESS_1=$bill_other_address_pay->address;
//    $bill_array['send_start_time'] = substr($bill_other_address_pay->startTime,0,5);
//    $bill_array['send_end_time'] = substr($bill_other_address_pay->endTime,0,5);
//    $PRTIME = ( $bill_array['send_end_time']=="00:00" || $bill_array['send_end_time']==$bill_array['send_start_time'] ) ? $bill_array['send_start_time'] : ( $bill_array['send_start_time'].'-'.$bill_array['send_end_time'] );//配送时间

}
if(strlen($PHONE)==11){
    $PHONE1=substr($PHONE,0,3);
    $PHONE2=substr($PHONE,3,4);
    $PHONE3=substr($PHONE,7,4);
    $PHONE=$PHONE1.'-'.$PHONE2.'-'.$PHONE3;
}

//add by yim.hu FUN-BK-0009  获取促销渠道 start
$bill_promotion = $bill_arraySearch->searchPromotion($bill_array['billPromotion']);
if(isset($bill_promotion[0]->itemName)){
	$bill_promotion = $bill_promotion[0]->itemName;
	if(strlen($bill_promotion)>40){
		$str = $bill_promotion;
		$bill_promotion = mb_substr($bill_promotion, 0, 14).'...';
		
	}
}else{
	$bill_promotion ='无';
}
//add by yim.hu FUN-BK-0009  获取促销渠道 end
$print_array = array(

    'bill' => array(
        'TOUBU' => $TOUBU,

        'PKID' => $bill_array['bill_code_substr'],
        'PRDATE1' => $send_date_1,

        'CREATEDATE' => $bill_array['createTime'],
        'PRTIME' => $PRTIME,

        'USERNAME' => $bill_array['distribution_technicia'],
        'SHIPNICK' => $bill_array['distribution_name'],

        'USERNICK' => $bill_array['custID'],
        'RECEIVPHONE' =>$PHONE,// $bill_array['deliver_mobile']?$bill_array['deliver_mobile']:$bill_array['deliver_phone'],

        'TOUBU2' =>$TOUBU2,
        'RECEIVNAME' => $RECEIVNAME_1,
        'RECEIVMOBILE1' =>$PHONE, // $bill_array['deliver_mobile']?$bill_array['deliver_mobile']:$bill_array['deliver_phone'],

        'TOUBU1' =>$TOUBU1,
        'ALL_RECEIVADDRESS' => $ALL_RECEIVADDRESS_1,

        'PAYTYPE' => $PAYTYPE,

        'ORDERAMOUNT' => $bill_array['bill_money'],
        'MAIXIN' => $bill_array['bill_receivable_money'],

        'OUTHERMESSAGE' => "",//$bill_array[''],//生日牌

        'LZFREE' =>0,//$bill_array[''],
        'LZNUM' => 0,//$bill_array[''],

        'SELLERMESSAGE' => $value=='礼盒'? $SELLERMESSAGE."[".$query[0]['itemName']."]":$SELLERMESSAGE,

        'TRADECOMMENT1' => $bill_array['invoice_title'],

        'TRADECOMMENT2' => $bill_array['invoice_content'],
        //mod 20141124 sunqiang  FUN-BK-0034 运费规则调整 start
        'BUYCHARGE' => '',
        //'BUYCHARGE' => $bill_array['deliver_fee'],
        //mod 20141124 sunqiang  FUN-BK-0034 运费规则调整 end
        'SHIPAMOUNT' => $bill_array['deliver_feePaid'],
        'QDTYPE' =>$bill_promotion,
        
    ),
    'totalCount'=>$bill_goods_total_count,
    'goods' => $goods_array

);

//生日牌组装成字符串
if( $bill_card_array ){
    $card_str = implode(',',$bill_card_array);
    $print_array['bill']['OUTHERMESSAGE'] = htmlspecialchars($card_str, ENT_QUOTES);
}

//赠送蜡烛数量
if( $bill_candle_amount ){
    $print_array['bill']['LZFREE'] = $bill_candle_amount;
}

//额外蜡烛数量
if( $bill_extra_candle_amount ){
    $print_array['bill']['LZNUM'] = $bill_extra_candle_amount;
}
//error_log(var_export($print_array,true));
$xml_string ="<xml>";
foreach($print_array['goods'] as &$goods) {
    $xml_string .="<row>";
    foreach($goods as $key => $val) {
        $xml_string .= "<$key>$val</$key>";
    }
    foreach($print_array['bill'] as $key => $val) {
        $xml_string .= "<$key>$val</$key>";
    }
    $xml_string .= "<TOTALCOUNT>".$print_array['totalCount']."</TOTALCOUNT>";
    $xml_string .="</row>";
}
$xml_string .="</xml>";

//header("gr_zip_type: deflate");                                      //指定压缩方法
//header("gr_zip_size: " . strval(strlen($xml_string)));                    //指定数据的原始长度
//error_log(iconv_get_encoding('internal_encoding'));
//header("gr_zip_encode: " . iconv_get_encoding('internal_encoding'));   //指定数据的编码方式 utf-8 utf-16 ...

//压缩数据并输出
//$compressed = gzdeflate($xml_string);
//echo base64_encode($compressed);

header("Content-Type: text/html; charset=utf-8"); //这里应根据实际运行环境调整，如：utf-8等

echo $xml_string;
